package com.ruif.client;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.ruif.dao.CarDAO;
import com.ruif.model.Car;
import com.ruif.service.ICarService;
import com.ruif.util.DBUtil;
import com.ruif.util.StringUtil;

public class CarServiceImpl implements ICarService
{
    CarDAO carDao = new CarDAO();
    DBUtil db = new DBUtil();
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public int listCar(DefaultTableModel dtm, String licenseNo)
    {
        Connection conn = null;
        try
        {
            conn = db.getConn();
            ResultSet rs = carDao.list(conn, licenseNo);
            while (rs.next())
            {
                Vector v = new Vector();
                v.add(rs.getString("id"));
                v.add(rs.getString("licenseNo"));
                v.add(rs.getString("price"));
                v.add(rs.getString("type"));
                v.add(rs.getString("date"));
                v.add(rs.getString("descrip"));
                dtm.addRow(v);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return 0;
            }
        }
        return 1;
    }

    @Override
    public int addCar(Car car)
    {
        if (StringUtil.isEmpty(car.getLicenseNo()))
        {
            return 0;
        }
        if (car.getDescrip().length() < 5)
        {
            return 0;
        }
        
        Connection conn = null;
        try
        {
            conn = db.getConn();
            return carDao.add(conn, car);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int modifyCar(Car car)
    {
        Connection conn = null;
        
        try
        {
            conn = db.getConn();
            return carDao.modify(conn, car);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int deleteCar(Car car)
    {
        if (StringUtil.isEmpty(car.getId() + ""))
        {
            JOptionPane.showMessageDialog(null, "请选择要删除的项！");
            return -1;
        }
        Connection conn = null;
        try
        {
            conn = db.getConn();
            return carDao.delete(conn, car);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
        finally
        {
            try
            {
                db.closeConn(conn);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return -1;
            }
        }
    }

}
